package com.sg.service.biz.pbff.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.service.biz.pbff.TemplateEngineService;
import com.wicket.okrcomponent.integration.TemplateEngineService;
import com.wicket.okrcomponent.integration.dto.*;
import com.wicket.okrframework.integration.AppSceneService;
import com.wicket.okrframework.integration.BehaviorTypeService;
import com.wicket.okrframework.integration.InductionRecordService;
import com.wicket.okrframework.integration.dto.*;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class TemplateEngineServiceImpl implements TemplateEngineService {

    @Resource
    com.sg.service.biz.component.TemplateEngineService fwCompTemplateEngineClient;
    @Resource
    com.sg.service.biz.framework.InductionRecordService fwBaseInductionRecordClient;
    @Resource
    com.sg.service.biz.framework.BehaviorTypeService fwBaseBehaviorTypeClient;
    @Resource
    com.sg.service.biz.framework.AppSceneService fwBaseAppSceneClient;
    @Resource
    private RedisUtil redisUtil;

    /**
     * B2-8新增语义引擎（平台）[1371]
     * gen by moon at 8/10/2022, 3:43:16 AM
     */
    @Trace(operationName = "B2-8新增语义引擎（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffAddSemanticEngineRespDto addSemanticEngine(BffAddSemanticEngineReqDto reqDto) {


        AddSemanticEngineRespDto addSemanticEngineRespDto_1 = null;
//步骤0: D2-8新增语义引擎 - addSemanticEngine
        AddSemanticEngineRespDto addSemanticEngineRespDto = null;
        AddSemanticEngineReqDto addSemanticEngineReqDto = new AddSemanticEngineReqDto();
        if (reqDto != null) {
            addSemanticEngineReqDto.setTemplateSemanticEngineCode(reqDto.getTemplateSemanticEngineCode());//sourceId:32105_1
            addSemanticEngineReqDto.setEmplateEngineName(reqDto.getEmplateEngineName());//sourceId:32106_1
            addSemanticEngineReqDto.setTemplateEngineTemplate(reqDto.getTemplateEngineTemplate());//sourceId:32107_1
            addSemanticEngineReqDto.setBehaviorTypeCode(reqDto.getBehaviorTypeCode());//sourceId:35165_1
            addSemanticEngineReqDto.setEngineTypeCode(reqDto.getEngineTypeCode());//sourceId:35166_1
            addSemanticEngineReqDto.setBizSceneId(reqDto.getBizSceneId());//sourceId:32496_1
            addSemanticEngineReqDto.setSampleContent(reqDto.getSampleContent());//sourceId:35167_1
            addSemanticEngineReqDto.setPictureSample(reqDto.getPictureSample());//sourceId:35168_1
            addSemanticEngineReqDto.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:32111_1
        }

        /*D2-8新增语义引擎[1367]   */
        Assert.isNull(addSemanticEngineReqDto.getTemplateSemanticEngineCode(), "B2-8新增语义引擎（平台）-D2-8新增语义引擎-模板引擎标识不能为空", false);
        Assert.isNull(addSemanticEngineReqDto.getEmplateEngineName(), "B2-8新增语义引擎（平台）-D2-8新增语义引擎-模板引擎名称不能为空", false);
        Assert.isNull(addSemanticEngineReqDto.getTemplateEngineTemplate(), "B2-8新增语义引擎（平台）-D2-8新增语义引擎- 模板引擎内容模板不能为空", false);
        Assert.isNull(addSemanticEngineReqDto.getBehaviorTypeCode(), "B2-8新增语义引擎（平台）-D2-8新增语义引擎-行为类型编码不能为空", false);
        Assert.isNull(addSemanticEngineReqDto.getBizSceneId(), "B2-8新增语义引擎（平台）-D2-8新增语义引擎-业务场景ID不能为空", false);
        Assert.isNull(addSemanticEngineReqDto.getSubjectLifeCycle(), "B2-8新增语义引擎（平台）-D2-8新增语义引擎-主体生命周期不能为空", false);
        addSemanticEngineRespDto = fwCompTemplateEngineClient.addSemanticEngine(addSemanticEngineReqDto).getData();

        addSemanticEngineRespDto_1 = addSemanticEngineRespDto;

        BffAddSemanticEngineRespDto retData = new BffAddSemanticEngineRespDto();
        if (addSemanticEngineRespDto_1 != null) {
            retData.setTemplateSemanticEngineId(addSemanticEngineRespDto_1.getTemplateSemanticEngineId());//sourceId:32113_1
        }


        return retData;
    }

    /**
     * B2-8查询语义引擎列表（平台）[1375]
     * gen by moon at 10/31/2022, 7:00:48 PM
     */
    @Trace(operationName = "B2-8查询语义引擎列表（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQuerySemanticEngineListRespDto querySemanticEngineList(BffQuerySemanticEngineListReqDto reqDto) {


        QuerySemanticEngineListRespDto querySemanticEngineListRespDto_1 = null;
        BatchQueryInductionRecordListComRespDto batchQueryInductionRecordListComRespDto_1 = null;
//步骤0: D2-8查语义引擎列表 - querySemanticEngineList
        QuerySemanticEngineListRespDto querySemanticEngineListRespDto = null;
        QuerySemanticEngineListReqDto querySemanticEngineListReqDto = new QuerySemanticEngineListReqDto();
        if (reqDto != null) {
            querySemanticEngineListReqDto.setEmplateEngineName(reqDto.getEmplateEngineName());//SimpleFieldAssign//sourceId:32531_1
            querySemanticEngineListReqDto.setBizSceneId(reqDto.getBizSceneId());//SimpleFieldAssign//sourceId:32534_1
            querySemanticEngineListReqDto.setIsArchive(reqDto.getIsArchive());//SimpleFieldAssign//sourceId:32536_1
        }

        /*D2-8查语义引擎列表[1370]   */
        Assert.isNull(querySemanticEngineListReqDto.getBizSceneId(), "B2-8查询语义引擎列表（平台）-D2-8查语义引擎列表-业务场景ID不能为空", false);
        Assert.isNull(querySemanticEngineListReqDto.getIsArchive(), "B2-8查询语义引擎列表（平台）-D2-8查语义引擎列表-是否存档不能为空", false);
        querySemanticEngineListRespDto = fwCompTemplateEngineClient.querySemanticEngineList(querySemanticEngineListReqDto).getData();


        querySemanticEngineListRespDto_1 = querySemanticEngineListRespDto;

//步骤1: D1-3批量查询就职记录列表（公共） - batchQueryInductionRecordListCom
        BatchQueryInductionRecordListComRespDto batchQueryInductionRecordListComRespDto = null;
        if (querySemanticEngineListRespDto != null) {
            BatchQueryInductionRecordListComReqDto batchQueryInductionRecordListComReqDto = new BatchQueryInductionRecordListComReqDto();
            if (querySemanticEngineListRespDto != null) {
                batchQueryInductionRecordListComReqDto.setPerOrgInductionRecordList(querySemanticEngineListRespDto.getSemanticEngineList().stream().map(item -> item.getOperationInductionId())
                        .collect(Collectors.toList()));/*list-to-strings*///sourceId:32247_1
            }

            /*D1-3批量查询就职记录列表（公共）[602]   */

            batchQueryInductionRecordListComRespDto = fwBaseInductionRecordClient.batchQueryInductionRecordListCom(batchQueryInductionRecordListComReqDto).getData();


            batchQueryInductionRecordListComRespDto_1 = batchQueryInductionRecordListComRespDto;
        }

        BffQuerySemanticEngineListRespDto retData = new BffQuerySemanticEngineListRespDto();
        //数据集融合  MoreListToOneSource
        if (querySemanticEngineListRespDto_1 != null && !CollectionUtil.isEmpty(querySemanticEngineListRespDto_1.getSemanticEngineList())) {
            for (SemanticEngineDto semanticEngineDto : querySemanticEngineListRespDto_1.getSemanticEngineList()) {
                BffSemanticEngineDto retElm = new BffSemanticEngineDto();
                if (querySemanticEngineListRespDto_1 != null) {
                    retElm.setTemplateSemanticEngineId(semanticEngineDto.getTemplateSemanticEngineId());//SimpleFieldAssign//sourceId:14206_2
                    retElm.setTemplateSemanticEngineCode(semanticEngineDto.getTemplateSemanticEngineCode());//SimpleFieldAssign//sourceId:14207_2
                    retElm.setEmplateEngineName(semanticEngineDto.getEmplateEngineName());//SimpleFieldAssign//sourceId:14208_2
                    retElm.setSubjectLifeCycle(semanticEngineDto.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:14213_2
                    retElm.setOperationInductionId(semanticEngineDto.getOperationInductionId());//SimpleFieldAssign//sourceId:14214_2
                    retElm.setOperateTime(semanticEngineDto.getOperateTime());//SimpleFieldAssign//sourceId:14215_2
                }
                retData.getSemanticEngineList().add(retElm);
                if (batchQueryInductionRecordListComRespDto_1 != null) {
                    for (PerOrgInductionRecordDto perOrgInductionRecordDto : batchQueryInductionRecordListComRespDto_1.getPerOrgInductionRecordList()) {
                        if (perOrgInductionRecordDto.getInductionRecordId().equals(semanticEngineDto.getOperationInductionId())) {
                            if (batchQueryInductionRecordListComRespDto_1 != null) {
                                retElm.setUserId(perOrgInductionRecordDto.getUserId());//SimpleFieldAssign//sourceId:38888_2
                                retElm.setUserFace(perOrgInductionRecordDto.getUserFace());//SimpleFieldAssign//sourceId:38891_2
                                retElm.setIdCardName(perOrgInductionRecordDto.getIdCardName());//SimpleFieldAssign//sourceId:38889_2
                                retElm.setNickName(perOrgInductionRecordDto.getNickName());//SimpleFieldAssign//sourceId:38890_2
                            }
                        }
                    }
                }

            }
        }//sourceId:32241_1


        retData.setPageNum(querySemanticEngineListRespDto.getPageNum());
        retData.setPageSize(querySemanticEngineListRespDto.getPageSize());
        retData.setTotal(querySemanticEngineListRespDto.getTotalNum());
        retData.setIsLastPage((Math.ceil(Double.valueOf(querySemanticEngineListRespDto.getTotalNum()) / Double.valueOf(querySemanticEngineListRespDto.getPageSize()))) <= retData.getPageNum());


        return retData;
    }

    /**
     * B2-8查询语义引擎详情[1374]
     * gen by moon at 10/28/2022, 11:08:54 PM
     */
    @Trace(operationName = "B2-8查询语义引擎详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffQuerySemanticEngineDetailRespDto querySemanticEngineDetail(BffQuerySemanticEngineDetailReqDto reqDto) {


        QuerySemanticEngineDetailRespDto querySemanticEngineDetailRespDto_1 = null;
        QueryBehaviorTypeDetailRespDto queryBehaviorTypeDetailRespDto_1 = null;
        QueryBizSceneDetailRespDto queryBizSceneDetailRespDto_1 = null;
        QueryBizAppSceneDetailRespDto queryBizAppSceneDetailRespDto_1 = null;
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto_1 = null;
//步骤0: D2-8查询语义引擎详情 - querySemanticEngineDetail
        QuerySemanticEngineDetailRespDto querySemanticEngineDetailRespDto = null;
        QuerySemanticEngineDetailReqDto querySemanticEngineDetailReqDto = new QuerySemanticEngineDetailReqDto();
        if (reqDto != null) {
            querySemanticEngineDetailReqDto.setTemplateSemanticEngineId(reqDto.getTemplateSemanticEngineId());//SimpleFieldAssign//sourceId:32210_1
        }

        /*D2-8查询语义引擎详情[1376]   */
        Assert.isNull(querySemanticEngineDetailReqDto.getTemplateSemanticEngineId(), "B2-8查询语义引擎详情-D2-8查询语义引擎详情-模板引擎ID 不能为空", false);
        querySemanticEngineDetailRespDto = fwCompTemplateEngineClient.querySemanticEngineDetail(querySemanticEngineDetailReqDto).getData();
        Assert.isTrue(querySemanticEngineDetailRespDto == null || querySemanticEngineDetailRespDto.getTemplateSemanticEngineId() == null, "返回值不能为空", false);

        querySemanticEngineDetailRespDto_1 = querySemanticEngineDetailRespDto;

//步骤1: D1-1查询行为类型详情 - queryBehaviorTypeDetail
        QueryBehaviorTypeDetailRespDto queryBehaviorTypeDetailRespDto = null;
        if (querySemanticEngineDetailRespDto != null) {
            QueryBehaviorTypeDetailReqDto queryBehaviorTypeDetailReqDto = new QueryBehaviorTypeDetailReqDto();
            if (querySemanticEngineDetailRespDto != null) {
                queryBehaviorTypeDetailReqDto.setBehaviorTypeCode(querySemanticEngineDetailRespDto.getBehaviorTypeCode());//SimpleFieldAssign//sourceId:34891_1
            }

            /*D1-1查询行为类型详情[720]   */
            Assert.isNull(queryBehaviorTypeDetailReqDto.getBehaviorTypeCode(), "B2-8查询语义引擎详情-D1-1查询行为类型详情-行为类型编码不能为空", false);
            queryBehaviorTypeDetailRespDto = fwBaseBehaviorTypeClient.queryBehaviorTypeDetail(queryBehaviorTypeDetailReqDto).getData();
            Assert.isTrue(queryBehaviorTypeDetailRespDto == null || queryBehaviorTypeDetailRespDto.getBehaviorTypeId() == null, "返回值不能为空", false);

            queryBehaviorTypeDetailRespDto_1 = queryBehaviorTypeDetailRespDto;
        }

//步骤2: D1-1查询业务场景详情 - queryBizSceneDetail
        QueryBizSceneDetailRespDto queryBizSceneDetailRespDto = null;
        if (querySemanticEngineDetailRespDto != null) {
            QueryBizSceneDetailReqDto queryBizSceneDetailReqDto = new QueryBizSceneDetailReqDto();
            if (querySemanticEngineDetailRespDto != null) {
                queryBizSceneDetailReqDto.setBizSceneId(querySemanticEngineDetailRespDto.getBizSceneId());//SimpleFieldAssign//sourceId:35124_1
            }

            /*D1-1查询业务场景详情[713]   */
            Assert.isNull(queryBizSceneDetailReqDto.getBizSceneId(), "B2-8查询语义引擎详情-D1-1查询业务场景详情-业务场景ID不能为空", false);
            queryBizSceneDetailRespDto = fwBaseAppSceneClient.queryBizSceneDetail(queryBizSceneDetailReqDto).getData();
            Assert.isTrue(queryBizSceneDetailRespDto == null || queryBizSceneDetailRespDto.getBizSceneId() == null, "返回值不能为空", false);

            queryBizSceneDetailRespDto_1 = queryBizSceneDetailRespDto;
        }

//步骤3: D1-1查询业务应用详情 - queryBizAppSceneDetail
        QueryBizAppSceneDetailRespDto queryBizAppSceneDetailRespDto = null;
        if (queryBizSceneDetailRespDto != null) {
            QueryBizAppSceneDetailReqDto queryBizAppSceneDetailReqDto = new QueryBizAppSceneDetailReqDto();
            if (queryBizSceneDetailRespDto != null) {
                queryBizAppSceneDetailReqDto.setBizAppId(queryBizSceneDetailRespDto.getBizAppId());//SimpleFieldAssign//sourceId:408183_1
            }

            /*D1-1查询业务应用详情[743]   */
            Assert.isNull(queryBizAppSceneDetailReqDto.getBizAppId(), "B2-8查询语义引擎详情-D1-1查询业务应用详情-业务应用ID不能为空", false);
            queryBizAppSceneDetailRespDto = fwBaseAppSceneClient.queryBizAppSceneDetail(queryBizAppSceneDetailReqDto).getData();


            queryBizAppSceneDetailRespDto_1 = queryBizAppSceneDetailRespDto;
        }

//步骤4: D1-3查询个人就职记录信息详情（公共） - queryInductionRecordInfoDetailCom
        QueryInductionRecordInfoDetailComRespDto queryInductionRecordInfoDetailComRespDto = null;
        if (querySemanticEngineDetailRespDto != null) {
            QueryInductionRecordInfoDetailComReqDto queryInductionRecordInfoDetailComReqDto = new QueryInductionRecordInfoDetailComReqDto();
            if (querySemanticEngineDetailRespDto != null) {
                queryInductionRecordInfoDetailComReqDto.setInductionRecordId(querySemanticEngineDetailRespDto.getOperationInductionId());//SimpleFieldAssign//sourceId:32249_1
            }

            /*D1-3查询个人就职记录信息详情（公共）[715]   */
            Assert.isNull(queryInductionRecordInfoDetailComReqDto.getInductionRecordId(), "B2-8查询语义引擎详情-D1-3查询个人就职记录信息详情（公共）-就职记录ID不能为空", false);
            queryInductionRecordInfoDetailComRespDto = fwBaseInductionRecordClient.queryInductionRecordInfoDetailCom(queryInductionRecordInfoDetailComReqDto).getData();


            queryInductionRecordInfoDetailComRespDto_1 = queryInductionRecordInfoDetailComRespDto;
        }

        BffQuerySemanticEngineDetailRespDto retData = new BffQuerySemanticEngineDetailRespDto();
        if (querySemanticEngineDetailRespDto_1 != null) {
            retData.setTemplateSemanticEngineId(querySemanticEngineDetailRespDto_1.getTemplateSemanticEngineId());//SimpleFieldAssign//sourceId:32212_1
            retData.setEmplateEngineName(querySemanticEngineDetailRespDto_1.getEmplateEngineName());//SimpleFieldAssign//sourceId:32214_1
            retData.setTemplateSemanticEngineCode(querySemanticEngineDetailRespDto_1.getTemplateSemanticEngineCode());//SimpleFieldAssign//sourceId:32213_1
            retData.setBehaviorTypeCode(querySemanticEngineDetailRespDto_1.getBehaviorTypeCode());//SimpleFieldAssign//sourceId:32216_1
            retData.setEntityId(querySemanticEngineDetailRespDto_1.getEntityId());//SimpleFieldAssign//sourceId:32218_1
            retData.setBizSceneId(querySemanticEngineDetailRespDto_1.getBizSceneId());//SimpleFieldAssign//sourceId:32502_1
            retData.setTemplateEngineTemplate(querySemanticEngineDetailRespDto_1.getTemplateEngineTemplate());//SimpleFieldAssign//sourceId:32215_1
            retData.setSampleContent(querySemanticEngineDetailRespDto_1.getSampleContent());//SimpleFieldAssign//sourceId:35137_1
            retData.setPictureSample(querySemanticEngineDetailRespDto_1.getPictureSample());//SimpleFieldAssign//sourceId:35138_1
            retData.setSubjectLifeCycle(querySemanticEngineDetailRespDto_1.getSubjectLifeCycle());//SimpleFieldAssign//sourceId:32219_1
            retData.setOperationInductionId(querySemanticEngineDetailRespDto_1.getOperationInductionId());//SimpleFieldAssign//sourceId:32221_1
            retData.setOperateTime(querySemanticEngineDetailRespDto_1.getOperateTime());//SimpleFieldAssign//sourceId:32222_1
        }
        if (queryBehaviorTypeDetailRespDto_1 != null) {
            retData.setBehaviorTypeName(queryBehaviorTypeDetailRespDto_1.getBehaviorTypeName());//SimpleFieldAssign//sourceId:32269_1
        }
        if (queryBizSceneDetailRespDto_1 != null) {
            retData.setBizSceneName(queryBizSceneDetailRespDto_1.getBizSceneName());//SimpleFieldAssign//sourceId:34714_1
            retData.setBizSceneCode(queryBizSceneDetailRespDto_1.getBizSceneCode());//SimpleFieldAssign//sourceId:408375_1
            retData.setBizAppId(queryBizSceneDetailRespDto_1.getBizAppId());//SimpleFieldAssign//sourceId:408714_1
        }
        if (queryBizAppSceneDetailRespDto_1 != null) {
            retData.setBizAppCode(queryBizAppSceneDetailRespDto_1.getBizAppCode());//SimpleFieldAssign//sourceId:408374_1
            retData.setBizAppName(queryBizAppSceneDetailRespDto_1.getBizAppName());//SimpleFieldAssign//sourceId:408376_1
        }
        if (queryInductionRecordInfoDetailComRespDto_1 != null) {
            retData.setUserFace(queryInductionRecordInfoDetailComRespDto_1.getUserFace());//SimpleFieldAssign//sourceId:32259_1
            retData.setIdCardName(queryInductionRecordInfoDetailComRespDto_1.getIdCardName());//SimpleFieldAssign//sourceId:32257_1
            retData.setNickName(queryInductionRecordInfoDetailComRespDto_1.getNickName());//SimpleFieldAssign//sourceId:32258_1
            retData.setUserId(queryInductionRecordInfoDetailComRespDto_1.getUserId());//SimpleFieldAssign//sourceId:32256_1
        }


        return retData;
    }

    /**
     * B2-8修改语义引擎（平台）[1372]
     * gen by moon at 8/17/2022, 8:03:55 PM
     */
    @Trace(operationName = "B2-8修改语义引擎（平台）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    public BffUpdateSemanticEngineRespDto updateSemanticEngine(BffUpdateSemanticEngineReqDto reqDto) {


        //步骤0: D2-8修改语义引擎 - updateSemanticEngine
        UpdateSemanticEngineRespDto updateSemanticEngineRespDto = null;
        UpdateSemanticEngineReqDto updateSemanticEngineReqDto = new UpdateSemanticEngineReqDto();
        if (reqDto != null) {
            updateSemanticEngineReqDto.setTemplateSemanticEngineId(reqDto.getTemplateSemanticEngineId());//sourceId:32133_1
            updateSemanticEngineReqDto.setTemplateSemanticEngineCode(reqDto.getTemplateSemanticEngineCode());//sourceId:32134_1
            updateSemanticEngineReqDto.setEmplateEngineName(reqDto.getEmplateEngineName());//sourceId:32135_1
            updateSemanticEngineReqDto.setTemplateEngineTemplate(reqDto.getTemplateEngineTemplate());//sourceId:32136_1
            updateSemanticEngineReqDto.setBehaviorTypeCode(reqDto.getBehaviorTypeCode());//sourceId:35196_1
            updateSemanticEngineReqDto.setEngineTypeCode(reqDto.getEngineTypeCode());//sourceId:35197_1
            updateSemanticEngineReqDto.setBizSceneId(reqDto.getBizSceneId());//sourceId:35199_1
            updateSemanticEngineReqDto.setSampleContent(reqDto.getSampleContent());//sourceId:35200_1
            updateSemanticEngineReqDto.setPictureSample(reqDto.getPictureSample());//sourceId:35201_1
            updateSemanticEngineReqDto.setSubjectLifeCycle(reqDto.getSubjectLifeCycle());//sourceId:32139_1
        }

        /*D2-8修改语义引擎[1369]   */
        Assert.isNull(updateSemanticEngineReqDto.getTemplateEngineTemplate(), "B2-8修改语义引擎（平台）-D2-8修改语义引擎- 模板引擎内容模板不能为空", false);
        updateSemanticEngineRespDto = fwCompTemplateEngineClient.updateSemanticEngine(updateSemanticEngineReqDto).getData();


        BffUpdateSemanticEngineRespDto retData = new BffUpdateSemanticEngineRespDto();


        return retData;
    }
    //
}
